package top.deepdesigner.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import top.deepdesigner.pojo.DtsCategory;
import top.deepdesigner.service.DtsCategoryService;
import top.deepdesigner.vo.CatVo;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Start up <br/>
 * Created with IntelliJ IDEA. <br/>
 * Project Name: shop_manage <br/>
 * Author: duanruiqing <br/>
 * DateTime: 2022/9/22 星期四 14:48:08 <br/>
 */
@Service
public class DtsCategoryServiceImpl extends ServiceImpl<BaseMapper<DtsCategory>, DtsCategory> implements DtsCategoryService {
  @Override
  public List<DtsCategory> getCategoryList() {
    LambdaQueryWrapper<DtsCategory> wrapper = new LambdaQueryWrapper<DtsCategory>().eq(true, DtsCategory::getDeleted, 0);
    return this.list(wrapper);
  }

  @Override
  public List<DtsCategory> getCategoryList(Integer page, Integer limit, String sort, String order, String id, String name) {
    return getPage(page, limit, sort, order, id, name).getRecords();
  }

  @Override
  public List<String> getCategoryLevelList() {
    LambdaQueryWrapper<DtsCategory> wrapper = new LambdaQueryWrapper<DtsCategory>()
        .eq(true, DtsCategory::getDeleted, 0)
        .select(DtsCategory::getLevel);
    List<DtsCategory> list = this.list(wrapper);
    return list.stream().map(DtsCategory::getLevel).distinct().collect(Collectors.toList());
  }

  @Override
  public Long getCategoryCount() {
    return this.count();
  }

  @Override
  public List<CatVo> getCategoryLevel(String level) {
    LambdaQueryWrapper<DtsCategory> wrapper = new LambdaQueryWrapper<>();
    wrapper.eq(!StringUtils.isEmpty(level), DtsCategory::getLevel, level);
    List<DtsCategory> list = this.list(wrapper);
    return list.stream().map(item -> {
      CatVo catVo = new CatVo();
      catVo.setValue(item.getId());
      catVo.setLabel(item.getName());
      return catVo;
    }).collect(Collectors.toList());
  }

  @Override
  @Transactional
  public DtsCategory createCategory(DtsCategory category) {
    List<DtsCategory> list = this.list(new LambdaQueryWrapper<DtsCategory>()
        .eq(!StringUtils.isEmpty(category.getName()), DtsCategory::getName, category.getName())
        .eq(true, DtsCategory::getDeleted, 0));
    if (list != null && list.size() > 0) {
      return null;
    }
    category.setAddTime(new Date());
    category.setUpdateTime(new Date());
    this.save(category);
    return category;
  }

  @Override
  @Transactional
  public Boolean updateCategory(DtsCategory category) {
    category.setUpdateTime(new Date());
    return this.update(category, new LambdaUpdateWrapper<DtsCategory>().eq(DtsCategory::getId, category.getId()));
  }

  @Override
  @Transactional
  public Boolean deleteCategory(DtsCategory category) {
    category.setDeleted(true);
    return this.update(category, new LambdaUpdateWrapper<DtsCategory>().eq(DtsCategory::getId, category.getId()));
  }

  //获取分页列表
  private Page<DtsCategory> getPage(Integer page, Integer limit, String sort, String order, String id, String name) {
    QueryWrapper<DtsCategory> wrapper = new QueryWrapper<>();
    wrapper
        .eq("deleted", 0)
        .like(!StringUtils.isEmpty(id), "id", id)
        .like(!StringUtils.isEmpty(name), "name", name)
        .orderBy(true, "asc".equalsIgnoreCase(order), sort);
    Page<DtsCategory> userPage = new Page<>(page, limit);
    return this.page(userPage, wrapper);
  }
}
